package com.woniuxueyuan.consumer;

import com.rabbitmq.client.Channel;
import com.woniuxueyuan.entity.Order;
import com.woniuxueyuan.mapper.OrderMapper;
import com.woniuxueyuan.util.RabbitmqConst;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 1：获取消息
 * 2：做业务逻辑
 *     判断用户的订单状态
 *       1：未付款状态：就要取消
 *       2：已付款，什么不做
 * 3：确认消息
 */

@Component
public class CheckOrderStatusConsumer {
    @Resource
    private OrderMapper orderMapper;
    @RabbitListener(queues = RabbitmqConst.CHECKORDERSTATUS_QUEUE)
    public void receiver(String msg, Channel channel, Message message){
        try {
            Order order = orderMapper.queryOrderInfoByOrderNum(msg);
            if(order.getStatus().equals(1)){
                //把订单状态改成取消
                Order order1 = new Order();
                order1.setOrderNum(msg);
                order1.setStatus(4);
                orderMapper.updateOrderStatus(order1);
            }

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
